//
//  DABorrowBookView.m
//  DAKnowledge
//
//  Created by DreamArts on 16/5/10.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DABorrowBookView.h"
#import "DALabel.h"
#import "Utility.h"
#import "DAHeader.h"

@interface DABorrowBookView ()
/** title数组*/
@property (nonatomic, strong) NSArray *titleArray;
/** button数组*/
@property (nonatomic, strong) NSMutableArray *buttonArray;

@property (nonatomic, copy) NSString *timeDuration;

@property (nonatomic, copy) NSString *privacy;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, copy) NSString *bookNumber;
@end

@implementation DABorrowBookView

- (instancetype)initWithFrame:(CGRect)frame numberArray:(NSArray *)array{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 5;
        self.backgroundColor = [UIColor colorWithHexString:backGroundColor];
        self.buttonArray = [NSMutableArray array];
        /** 创建scrollView*/
        self.scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(20, 80, self.frame.size.width - 40, 40)];
        //self.scrollView.contentSize = CGSizeMake(500 * 2, 30);
        self.scrollView.bounces = NO;
        self.scrollView.showsHorizontalScrollIndicator = YES;
        self.scrollView.layer.cornerRadius = 5;
       // self.scrollView.layer.borderWidth = 1.0;
      //  self.scrollView.layer.borderColor = [UIColor supportGreenColor].CGColor;
        
        [self addSubview:self.scrollView];
        self.bookNumberArray = array;
        /** 创建图书编号*/
        for (int i = 0; i <self.bookNumberArray.count ; i ++) {
            [self creatBookNumberButton:self.bookNumberArray[i] index:i];
        }
        self.scrollView.contentSize = CGSizeMake((self.frame.size.width - 60)/ 3 * (self.bookNumberArray.count + 1), 40);
        NSString *isHaveNotBook = NSLocalizedString(@"thisBookHavenNot", nil);
        if (self.bookNumberArray.count == 0) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, self.scrollView.frame.size.width, 30)];
            label.text = isHaveNotBook;
            label.textColor = [UIColor colorWithHexString:bookNameColor];
            label.font = [UIFont systemFontOfSize:12];
            [self.scrollView addSubview:label];
        }
        /** 借阅时间label*/
        NSString *borrowTimeStr = NSLocalizedString(@"borrowTimeLabel", nil);
        self.borrowTimeLabel = [[DALabel alloc]initWithFrame:CGRectMake(20, 130, 100, 20)];
        [self.borrowTimeLabel setText:borrowTimeStr fitToContent:YES];
        self.borrowTimeLabel.font = [UIFont systemFontOfSize:12];
        self.borrowTimeLabel.textColor = [UIColor colorWithHexString:userNameColor];
        [self addSubview:self.borrowTimeLabel];
        
        NSString *oneWeek = NSLocalizedString(@"OneWeek", nil);
        NSString *twoWeek = NSLocalizedString(@"twoWeek", nil);
        NSString *threeWeek = NSLocalizedString(@"threeWeek", nil);
        NSString *oneMonth = NSLocalizedString(@"OneMonth", nil);
        NSString *twoMonth = NSLocalizedString(@"twoMonth", nil);
        NSString *threeMonth = NSLocalizedString(@"threeMonth", nil);
        
        /** 循环创建时间按钮*/
        self.titleArray = @[oneWeek, twoWeek, threeWeek, oneMonth, twoMonth, threeMonth];
        for (int i = 0; i < 6; i++) {
            [self creatTimeButtonWithTitle:self.titleArray[i] index:i];
        }
        /** 是否设成隐私label*/
        UIButton *button = self.buttonArray[4];
        self.privacyLabel = [[DALabel alloc]initWithFrame:CGRectMake(20, button.frame.origin.y + button.frame.size.height + 10, 150, 20)];
        NSString *isPrivacy = NSLocalizedString(@"isSetPrivacy", nil);
        [self.privacyLabel setText:isPrivacy fitToContent:YES];
        self.privacyLabel.font = [UIFont systemFontOfSize:12];
        self.privacyLabel.textColor = [UIColor colorWithHexString:userNameColor];
        [self addSubview:self.privacyLabel];
        /** switch开关*/
        self.privacySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(self.frame.size.width - 20 - 50, self.privacyLabel.frame.origin.y, 50, 20)];
        [self.privacySwitch addTarget:self action:@selector(changePrivacy) forControlEvents:UIControlEventValueChanged];
        self.privacy = @"1";
        [self addSubview:self.privacySwitch];
        /** 借阅按钮*/
        self.borrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.borrowButton.frame = CGRectMake(20, self.privacySwitch.frame.origin.y + self.privacySwitch.frame.size.height + 20, self.frame.size.width - 40, 40);
        if (self.bookNumberArray.count == 0) {
            self.borrowButton.backgroundColor = [UIColor colorWithHexString:backNowColor];
            self.borrowButton.userInteractionEnabled = NO;
        }else{
            self.borrowButton.backgroundColor = [UIColor supportGreenColor];
            self.borrowButton.userInteractionEnabled = YES;
        }
        self.borrowButton.layer.cornerRadius = 5;
        NSString *borrowStr = NSLocalizedString(@"isBorrow",nil);
        [self.borrowButton setTitle:borrowStr forState:UIControlStateNormal];
        [self.borrowButton addTarget:self action:@selector(clickBorrowButton:) forControlEvents:UIControlEventTouchUpInside];
        self.borrowButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.borrowButton];
        /** 取消image*/
        self.cancelImage = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width - 25, 5, 20, 20)];
        self.cancelImage.image = [UIImage imageNamed:@"close"];
        [self addSubview:self.cancelImage];
        self.cancelImage.userInteractionEnabled = YES;
        /** 给image添加手势*/
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self.cancelImage addGestureRecognizer:tap];
    }
    return self;
}
#pragma mark - 创建时间按钮
- (void)creatTimeButtonWithTitle:(NSString *)title index:(int)index{
    CGFloat buttonWidth = (self.frame.size.width - 60)/ 3;
    CGFloat buttonHeight = 30;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:title forState:UIControlStateNormal];
    button.frame = CGRectMake(20 + (buttonWidth + 10) * (index % 3), self.borrowTimeLabel.frame.origin.y + self.borrowTimeLabel.frame.size.height + 10 + (buttonHeight + 10) * (index / 3), buttonWidth, buttonHeight);
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button setTitleColor:[UIColor colorWithHexString:lostButtonColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chooseBorrowTimeButton:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = index + 1000;
    button.layer.cornerRadius = 5;
    [self addSubview:button];
    [self.buttonArray addObject:button];
}
#pragma mark - 图片手势方法
- (void)tapAction:(UITapGestureRecognizer *)tap{
    [self.delegate viewDisapper];
}
#pragma mark - 时间按钮点击方法
- (void)chooseBorrowTimeButton:(UIButton *)btn{
    for (int i = 0 ; i < 6; i++) {
        UIButton *button  = (UIButton *)[self viewWithTag:i + 1000];
        button.layer.borderWidth = 0;
    }
    UIButton *button = (UIButton *)[self viewWithTag:btn.tag];
    button.layer.borderColor = [UIColor supportRedColor].CGColor;
    button.layer.borderWidth = 1;
    button.clipsToBounds = YES;
    
    NSString *oneWeek = NSLocalizedString(@"OneWeek", nil);
    NSString *twoWeek = NSLocalizedString(@"twoWeek", nil);
    NSString *threeWeek = NSLocalizedString(@"threeWeek", nil);
    NSString *oneMonth = NSLocalizedString(@"OneMonth", nil);
    NSString *twoMonth = NSLocalizedString(@"twoMonth", nil);
    NSString *threeMonth = NSLocalizedString(@"threeMonth", nil);
    
    if ([button.titleLabel.text isEqualToString:oneWeek]) {
        self.timeDuration = @"1w";
    }
    if ([button.titleLabel.text isEqualToString:twoWeek]) {
        self.timeDuration = @"2w";
    }
    if ([button.titleLabel.text isEqualToString:threeWeek]) {
        self.timeDuration = @"3w";
    }
    if ([button.titleLabel.text isEqualToString:oneMonth]) {
        self.timeDuration = @"1m";
    }
    if ([button.titleLabel.text  isEqualToString:twoMonth]) {
        self.timeDuration = @"2m";
    }
    if ([button.titleLabel.text isEqualToString:threeMonth]) {
        self.timeDuration = @"3m";
    }
    NSLog(@"%@", self.timeDuration);
}
#pragma mark - 借阅按钮点击方法
- (void)clickBorrowButton:(UIButton *)btn{
    NSLog(@"借阅");
    [self.delegate toBorrowBookWithTime:self.timeDuration
                                Privacy:self.privacy
                             BookNumber:self.bookNumber];
}
#pragma switch 方法
- (void)changePrivacy{
    if ([self.privacySwitch isOn]) {
        self.privacy = @"0";
    }else{
        self.privacy = @"1";
    }
}
#pragma mark - 创建图书编号button
- (void)creatBookNumberButton:(NSString *)title index:(int)index{
    CGFloat buttonWidth = (self.frame.size.width - 60)/ 3;
    CGFloat buttonHeight = 30;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:title forState:UIControlStateNormal];
    button.frame = CGRectMake((buttonWidth + 10) * index + 5, 5, buttonWidth, buttonHeight);
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button setTitleColor:[UIColor colorWithHexString:lostButtonColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickBookNumberButton:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = index + 2000;
    button.layer.cornerRadius = 5;
    [self.scrollView addSubview:button];
}

- (void)clickBookNumberButton:(UIButton *)btn{
    for (int i = 0 ; i < self.bookNumberArray.count; i++) {
        UIButton *button  = (UIButton *)[self viewWithTag:i + 2000];
        button.layer.borderWidth = 0;
    }
    UIButton *button = (UIButton *)[self viewWithTag:btn.tag];
    button.layer.borderColor = [UIColor supportRedColor].CGColor;
    button.layer.borderWidth = 1;
    button.clipsToBounds = YES;
    
    /** 获取图书编号*/
    self.bookNumber = button.titleLabel.text;
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
